;
(function($) {
	"use strict";

	var oCart = {
		offsetTop: $(".cart-toolbar").offset().top,
		// 全选状态
		toggleAll: function() {
			var that = this,
				bChecked = true;
			$("span[data-checkbox='all']").each(function() {
				that.toggleChecked($(this));
				bChecked = $(this).hasClass("ticks");
				that.checked($(this), bChecked);
			});

			//更新"店铺"、"商品"选中状态
			$("span[data-checkbox='shop'], span[data-checkbox='single']").each(function() {
				that.toggleChecked($(this), bChecked);
				that.checked($(this), bChecked);
			});

			//计算选中商品总价
			that.countSumPrice();
			that.toggleActive();
		},

		// "店铺"选中状态
		toggleShop: function(self) {
			var that = this;
			that.toggleChecked(self);

			//更新"商品"的选中状态
			var bChecked = self.hasClass("ticks");
			that.checked(self, bChecked);
			self.parents(".goodBlock").find(".goods-container span[data-checkbox='single']").each(function() {
				that.toggleChecked($(this), bChecked);
				that.checked($(this), bChecked);
			});

			//更新"全选"状态
			var jSelectAll = $("span[data-checkbox='all']");
			that.toggleChecked(jSelectAll, that.checkAllSelect(2));

			//计算选中商品总价
			that.countSumPrice();
			that.toggleActive();
		},

		// "商品"选中状态
		toggleSingle: function(self) {
			var that = this;
			that.toggleChecked(self);

			var bChecked = self.hasClass("ticks"),
				oShopSelect = self.parents(".goodBlock").find("span[data-checkbox='shop']"),
				jSelectAll = $("span[data-checkbox='all']");
			//更新"店铺"选中状态
			that.toggleChecked(oShopSelect, bChecked && that.checkAllSelect(1, self));
			//更新"全选"选中状态
			that.toggleChecked(jSelectAll, bChecked && that.checkAllSelect(2));

			//计算选中商品总价
			that.countSumPrice();
			that.toggleActive();
		},

		// type:商品品1、店铺2
		checkAllSelect: function(type, o) {
			var bFlag = true;

			switch (type) {
				case 1:
					if (o instanceof $) {
						o.parents(".goodBlock").find(".goods-container span[data-checkbox='single']").each(function() {
							return $(this).hasClass("ticks") ? true : bFlag = false;
						})
					}
					break;
				case 2:
					$(".myShopping").find("span[data-checkbox='shop']").each(function() {
						return $(this).hasClass("ticks") ? true : bFlag = false;
					})
					break;
				default:
					bFlag = false;
			}

			return bFlag;
		},

		// 设置隐藏checkbox选中状态
		checked: function(o, flag) {
			if (o instanceof $) {
				o.next("input[type='checkbox']").prop("checked", flag && true);
			}
		},

		// 切换"选中"状态
		toggleChecked: function(o, flag) {
			if (!o instanceof $) return;

			if (arguments.length == 1) {
				o.toggleClass("ticks");
			} else {
				flag ? o.addClass("ticks") : o.removeClass("ticks");
			}
		},

		// 切换"选中"商品背景色
		toggleActive: function() {
			$("span[data-checkbox = 'single']").parents(".goods-container").removeClass("active");
			$("span[data-checkbox='single'][class~='ticks']").parents(".goods-container").addClass("active");
		},

		// 增加商品数量+
		addSkuNum: function(self) {
			var that = this,
				jGoodNum = self.prev(),
				jBtnReduce = self.prevAll(".btn_reduce"),
				sCount = jGoodNum.val();

			if (self.hasClass("disabled")) return;

			sCount = parseInt(that.handleSkuNum(sCount)) + 1;

			if (sCount === 2) {
				jBtnReduce.removeClass("disabled");
			} else if (that.checkSkuNum(sCount) !== true) { //校验库存是否允许
				self.addClass("disabled");
			}
			jGoodNum.val(sCount);

			//计算商品总价
			that.countSkuPrice(self.parents(".goods-container"));
			//计算选中商品总价
			that.countSumPrice();
		},

		// 减少商品数量-
		reduceSkuNum: function(self) {
			var that = this,
				jGoodNum = self.next(),
				jBtnAdd = self.nextAll(".btn_add"),
				sCount = jGoodNum.val();

			sCount = parseInt(that.handleSkuNum(sCount));

			if (self.hasClass("disabled") || sCount === 1) return;

			if (sCount === 2) {
				self.addClass("disabled");
			}

			jGoodNum.val(sCount - 1);
			jBtnAdd.removeClass("disabled");

			//计算商品总价
			that.countSkuPrice(self.parents(".goods-container"));
			//计算选中商品总价
			that.countSumPrice();
		},

		// 处理商品数量
		handleSkuNum: function(count) {
			var nFlag = this.validSkuNum(count);
			if (nFlag === 0) {
				return count.replace("0", "");
			} else if (nFlag === -1) {
				return 1;
			} else {
				return count;
			}
		},

		// 校验商品数量是否符合规则
		validSkuNum: function(count) {
			if (/^[1-9][0-9]?$/.test(count)) {
				return 1;
			} else if (/^0[1-9]$/.test(count)) {
				return 0;
			} else {
				return -1;
			}
		},

		// 校验商品数量是否满足库存
		checkSkuNum: function(count) {
			var nResult = 3;
			if (count >= nResult) {
				return nResult;
			} else {
				return true;
			}
		},

		toggleUnit: function(str, unit, flag) {
			return flag ? parseFloat(str.replace(unit, "")) : (str + unit);
		},

		// 更新单个商品总价
		countSkuPrice: function(self) {
			if (self instanceof $ && self.hasClass("goods-container")) {
				var sUnitPrice = this.toggleUnit(self.find(".singleP").text(), "元", true),
					sUnitPoint = this.toggleUnit(self.find(".singleJF").text(), "积分", true),
					nCount = parseInt(self.find(".good_number").val()),
					jSumPrice = self.find(".totalP"),
					jSumPoint = self.find(".totalJF");

				jSumPrice.text(this.toggleUnit(nCount * sUnitPrice, "元", false));
				jSumPoint.text(this.toggleUnit(nCount * sUnitPoint, "积分", false));
			}
		},

		// 更新商品总价和积分
		countSumPrice: function() {
			var jGoods = $(" span[data-checkbox = 'single'][class~='ticks']"),
				jParent = null,
				nLength = jGoods.length,
				nSumPrice = 0.00,
				nSumPoints = 0;

			//更新选中商品数量
			$(".amount-sum span").text(nLength);

			//更新选中商品的总价及积分
			while (nLength) {
				jParent = jGoods.eq(--nLength).parents(".goods-container");
				nSumPrice += this.toggleUnit(jParent.find(".totalP").text(), "元", true);
				nSumPoints += this.toggleUnit(jParent.find(".totalJF").text(), "元", true);
			}
			nSumPrice = nSumPrice || "0.00";
			$(".sum_price").text(nSumPrice);
			$(".sum_point").text(nSumPoints);
		},

		// 固定结算栏
		fixedToolbar: function() {
			var jToolbar = $(".cart-toolbar"),
				nHeight = jToolbar.height(),
				nClientHeight = $(window).height(), //可视区域高度
				nScrollTop = $(window).scrollTop(); //滚动条高度

			if (nScrollTop + nClientHeight - nHeight >= this.offsetTop) {
				jToolbar.attr("style", "");
				return;
			} else if (jToolbar.css("position") === "static") {
				jToolbar.css({
					position: "fixed",
					bottom: 0,
					left: "50%",
					marginLeft: "-" + jToolbar.width() / 2 + "px"
				})
			}
		}
	}

	//商品区
	$(".myShopping").on("click", function(e) {
		var sNodeName = e.target.nodeName.toLowerCase(),
			jSelf = $(e.target);

		//更新"选中"状态
		if (sNodeName === "span" && jSelf.data("checkbox")) {
			switch (jSelf.data("checkbox")) {
				case "all": //全选
					oCart.toggleAll();
					break;
				case "shop": //"店铺"全选
					oCart.toggleShop(jSelf);
					break;
				case "single":
					oCart.toggleSingle(jSelf);
					break;
				default:
					return;
			}
		}

		// 更新商品数量以及删除商品
		if (sNodeName === "a" && jSelf.data("btn-type")) {
			switch (jSelf.data("btn-type")) {
				case "reduce": // "-"
					oCart.reduceSkuNum(jSelf);
					break;
				case "add": // "+"
					oCart.addSkuNum(jSelf);
					break;
				case "delete": // 删除商品
					ECar.modal.confirm({
						content: "是否删除当前商品?",
						okFn: function() {
							//删除商品


							ECar.modal.alert({
								content: "删除成功!"
							});
						}
					});
					break;
				default:
					return;
			}
		}
	});

	//结算区
	$(".cart-toolbar").on("click", function(e) {
		var sNodeName = e.target.nodeName.toLowerCase(),
			jSelf = $(e.target);

		//全选
		if (sNodeName === "span" && jSelf.data("checkbox") == "all") {
			oCart.toggleAll();
		}

		// 删除商品
		if (sNodeName === "a" && jSelf.data("btn-type") === "delete") {
			ECar.modal.confirm({
				content: "是否删除当前选中商品?",
				okFn: function() {
					//删除商品
					ECar.modal.alert({
						content: "删除成功!"
					});
				}
			});
		}
	});

	//校验输入的商品数量
	$(".good_number").on("keyup", function(e) {
		var sKeyCode = e.which || e.keyCode,
			sCount = $(this).val();

		if ((sKeyCode >= 96 && sKeyCode <= 105) || (sKeyCode >= 48 && sKeyCode <= 57)) {
			return;
		}

		sCount = sCount.length === 1 ? "1" : sCount.slice(0, 1);
		$(this).val(sCount);
	});
	$(".good_number").on("blur", function(e) {
		var sCount = $(this).val(),
			jBtnReduce = $(this).prev(),
			jBtnAdd = $(this).next();

		sCount = parseInt(oCart.handleSkuNum(sCount));
		var result = oCart.checkSkuNum(sCount);

		(sCount === 1 || result === 1) ? jBtnReduce.addClass("disabled"): jBtnReduce.removeClass("disabled");

		if (result === true) {
			$(this).val(sCount);
			jBtnAdd.removeClass("disabled");
		} else {
			$(this).val(result);
			jBtnAdd.addClass("disabled");
		}

		//计算商品总价
		oCart.countSkuPrice($(this).parents(".goods-container"));
	});

	$(window).scroll(function() {
		oCart.fixedToolbar();
	});


	//setTop
	var mixInfo = $('.mixInfo'),
		mixOperation = $('.mix .mixOperation'),
		cartCheckbox = $('.mix .cart-checkbox'),
		loseTxt = $('.mix .loseTxt');
	mixInfo.each(function(i){
		var $this = $(this),
			mixHeight = $this.height(),
			mixOperationHeight=mixOperation.eq(i).height(),
			toTop=(mixHeight-mixOperationHeight)/2;

			mixOperation.eq(i).css({'marginTop':toTop+'px'});
			loseTxt.eq(i).css({'marginTop':mixHeight-16+'px'});
			cartCheckbox.eq(i).css({'line-height':mixHeight+'px'});
	});




})(jQuery);